#include "gtest/gtest.h"
#include "StringUtil.h"

using namespace Util;

/*
 * 简述：
 *      Spilt空字符串时返回0
 *
 * 输入参数:
 *      Line    =>      空字符串   
 *          
 * 预期结果:
 *      return 0
 */
TEST(TestSplit, SplitEmptyStringRetrunZero)
{
    string          Line;
    vector<string>  vecItem;

    EXPECT_EQ( -1, Split(Line, vecItem, ",", 0));
    EXPECT_TRUE( vecItem.empty() );

    EXPECT_EQ( -1, Split(Line, vecItem, ',', 0));
    EXPECT_TRUE( vecItem.empty() );
}

/*
 * 简述:
 *      Spilt一个元素时返回1
 *
 * 输入参数：
 *      Line    =>  "a,b,c,d,e"
 *
 * 预期结果:
 *      return 1
 *      vecItem[0] == 'a'
 */
TEST(TestSplit, SplitOneCountReturnOne)
{
    string          Line="a,b,c,d,e";
    vector<string>  vecItem;

    EXPECT_EQ( 1, Split(Line, vecItem, ",", 1) );
    EXPECT_EQ( 1, static_cast<int>( vecItem.size() ) );
    EXPECT_STREQ( "a", vecItem[0].c_str() );

    vecItem.clear();
    EXPECT_TRUE( vecItem.empty() );

    EXPECT_EQ( 1, Split(Line, vecItem, ',', 1) );
    EXPECT_EQ( 1, static_cast<int>( vecItem.size() ) );
    EXPECT_STREQ( "a", vecItem[0].c_str() );
}

/*
 * 简述:
 *      Split5以多个字符分隔的字符串
 *
 * 输入参数:
 *      Line    =>  "a<x>b<x>c<x>d<x>e"
 *
 * 预期结果:
 *      return 5
 *      vecItem[0] == 'a'
 *      vecItem[1] == 'b'
 *      vecItem[2] == 'c'
 *      vecItem[3] == 'd'
 *      vecItem[4] == 'e'
 */
TEST(TestSplit, SplitFiveCountReturnFiveWithMultiCharDelimiter)
{
    string          Line="a<x>b<x>c<x>d<x>e";
    vector<string>  vecItem;

    EXPECT_EQ( 5, Split(Line, vecItem, "<x>", 5) );
    EXPECT_EQ( 5, vecItem.size() );
    EXPECT_STREQ( "a", vecItem[0].c_str() );
    EXPECT_STREQ( "b", vecItem[1].c_str() );
    EXPECT_STREQ( "c", vecItem[2].c_str() );
    EXPECT_STREQ( "d", vecItem[3].c_str() );
    EXPECT_STREQ( "e", vecItem[4].c_str() );

}


 
/*
 * 简述：
 *      给定5个元素，并使用Split进行5次切割
 *
 * 输入参数：
 *      Line    =>  "a,b,c,d,e"
 *
 * 预期结果:
 *      return 5
 *
 *      vecItem[0] == 'a'
 *      vecItem[1] == 'b'
 *      vecItem[2] == 'c'
 *      vecItem[3] == 'd'
 *      vecItem[4] == 'e'
 */
TEST(TestSplit, SplitFiveCountReturnFive)
{
    string          Line="a,b,c,d,e";
    vector<string>  vecItem;

    EXPECT_EQ( 5, Split(Line, vecItem, ",", 5) );
    EXPECT_EQ( 5, vecItem.size() );
    EXPECT_STREQ( "a", vecItem[0].c_str() );
    EXPECT_STREQ( "b", vecItem[1].c_str() );
    EXPECT_STREQ( "c", vecItem[2].c_str() );
    EXPECT_STREQ( "d", vecItem[3].c_str() );
    EXPECT_STREQ( "e", vecItem[4].c_str() );

    vecItem.clear();
    EXPECT_TRUE( vecItem.empty() );

    EXPECT_EQ( 5, Split(Line, vecItem, ',', 5) );
    EXPECT_EQ( 5, vecItem.size() );
    EXPECT_STREQ( "a", vecItem[0].c_str() );
    EXPECT_STREQ( "b", vecItem[1].c_str() );
    EXPECT_STREQ( "c", vecItem[2].c_str() );
    EXPECT_STREQ( "d", vecItem[3].c_str() );
    EXPECT_STREQ( "e", vecItem[4].c_str() );
}

/*
 * 简述:
 *      给定5个元素，并使用Split进行6次切割
 *
 * 输入参数：
 *      Line    =>  "a,b,c,d,e"
 *      Count   =>  6
 *
 * 预期结果：
 *      return 5
 *
 *      vecItem[0] == 'a'
 *      vecItem[1] == 'b'
 *      vecItem[2] == 'c'
 *      vecItem[3] == 'd'
 *      vecItem[4] == 'e'
 */
TEST(TestSplit, EX_SplitSixCountReturnFive) 
{
    string          Line="a,b,c,d,e";
    vector<string>  vecItem;

    EXPECT_EQ( 5, Split(Line, vecItem, ",", 6) );
    EXPECT_STREQ( "a", vecItem[0].c_str() );
    EXPECT_STREQ( "b", vecItem[1].c_str() );
    EXPECT_STREQ( "c", vecItem[2].c_str() );
    EXPECT_STREQ( "d", vecItem[3].c_str() );
    EXPECT_STREQ( "e", vecItem[4].c_str() );

    vecItem.clear();
    EXPECT_TRUE( vecItem.empty() );

    EXPECT_EQ( 5, Split(Line, vecItem, ',', 6) );
    EXPECT_STREQ( "a", vecItem[0].c_str() );
    EXPECT_STREQ( "b", vecItem[1].c_str() );
    EXPECT_STREQ( "c", vecItem[2].c_str() );
    EXPECT_STREQ( "d", vecItem[3].c_str() );
    EXPECT_STREQ( "e", vecItem[4].c_str() );

}

/*
 * 简述:
 *     输入负的Count数 
 *
 * 输入参数:
 *      Line    =>  "a,b,c,d,e"
 *      Count   => -1
 *
 * 预期结果:
 *      return 0
 *      vecItem.empty() == true
 */
TEST(TestSplit, EX_SplitNegativeCountReturnZero)
{
    string          Line = "a,b,c,d,e";
    vector<string>  vecItem;
    EXPECT_EQ( 0, Split(Line, vecItem, ",", -1) );
    EXPECT_TRUE( vecItem.empty() );

    EXPECT_EQ( 0, Split(Line, vecItem, ',', -1) );
    EXPECT_TRUE( vecItem.empty() );
}

/*
 * 简述:
 *      输入空的字符串，Count设置为1个
 *
 * 输入参数:
 *      Line    =>  ""
 *      Count   =>  1
 *
 * 预期结果：
 *      return 0
 *      vecItem.empty() == true
 */

TEST(TestSplit, EX_SplitEmptyStringOneCountReturnZero)
{
    string          Line;
    vector<string>  vecItem;
    EXPECT_EQ( -1, Split(Line, vecItem, ",", 1) );
    EXPECT_TRUE( vecItem.empty() );


    EXPECT_EQ( -1, Split(Line, vecItem, ',', 1) );
    EXPECT_TRUE( vecItem.empty() );
}
